<?php
/**
 * Data model
 *
 * @category  DataModel
 * @package   DataModel
 * @author    DataModel Generator v1.0
 * @version   $Revision: 127 $
 */
/**
 * Data model for table "access_users"
 *
 * @category  DataModel
 * @package   DataModel
 * @author    DataModel Generator v1.0
 * @version   $Revision: 127 $
 */

//using('Framework.System.Auth');
//using('Framework.System.Google.Auth');

class User extends BaseUser// implements IUser, IGoogleUser
{
    private $_settings = null;

    public static function getUserById($id)
    {
        return User::getById(intval($id));
    }

    public static function passwordHash($password)
    {
        return hash('sha256', $password);
    }

    public static function getUserByLoginPassword($login, $password)
    {

        $pass = self::passwordHash($password);
        $q = ORMQuery::selectBuilder();
        $q->from('User')
          ->where('login = ? AND passwd = ?', array($login, $pass));

        return $q->fetch('User');
    }

    public static function getUserByGoogleMail($email)
    {
        $q = ORMQuery::selectBuilder();
        $q->from('User')
          ->where('google_id = ?', $email);

        return $q->fetch('User');
    }

    public function &getSettings()
    {
        if ($this->_settings == null && !empty($this->settings)) {
            $this->_settings = (object) unserialize($this->settings);
        }
        if ($this->_settings == null) {
            $this->_settings = new stdClass;
        }
        return $this->_settings;
    }

    public function getSetting($name)
    {
        $sett = $this->getSettings();
        return $sett->$name;
    }

    public function save()
    {
        $sett = (array)$this->_settings;
        unset($sett['scalar']);
        $this->settings = serialize($sett);
        parent::save();
    }

    public function getAvatar()
    {
        return $this->avatar;
    }

    public static function saveIfNotExists($params = null)
    {
        return parent::saveIfNotExists($params, __CLASS__);
    }
}
